package com.xxx.sms; /**
 * Created by bingone on 15/12/16.
 */

import com.xxx.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
 * 短信http接口的java代码调用示例
 * 基于Apache HttpClient 4.3
 *
 * @author songchao
 * @since 2015-04-03
 */
@Slf4j
public class SmsUtil {

    /**
     *查账户信息的http地址
     */
    private static String URI_GET_USER_INFO =
        "https://sms.yunpian.com/v2/user/get.json";

    /**
     *智能匹配模板发送接口的http地址
     */
    private static String URI_SEND_SMS =
        "https://sms.yunpian.com/v2/sms/single_send.json";

    /**
     *模板发送接口的http地址
     */
    private static String URI_TPL_SEND_SMS =
        "https://sms.yunpian.com/v2/sms/tpl_single_send.json";

    /**
     *发送语音验证码接口的http地址
     */
    private static String URI_SEND_VOICE =
        "https://voice.yunpian.com/v2/voice/send.json";

    /**
     *绑定主叫、被叫关系的接口http地址
     */
    private static String URI_SEND_BIND =
        "https://call.yunpian.com/v2/call/bind.json";

    /**
     *解绑主叫、被叫关系的接口http地址
     */
    private static String URI_SEND_UNBIND =
        "https://call.yunpian.com/v2/call/unbind.json";

    /**
     * 编码格式。发送编码格式统一用UTF-8
     */
    private static String ENCODING = "UTF-8";





    /**
     * 取账户信息
     *
     * @return json格式字符串
     * @throws java.io.IOException
     */

    public static String getUserInfo(String apikey) throws IOException,
        URISyntaxException {
            Map < String, String > params = new HashMap < String, String > ();
            params.put("apikey", apikey);
            return HttpUtil.post(URI_GET_USER_INFO, params);
        }

    /**
     * 智能匹配模板接口发短信
     *
     * @param apikey apikey
     * @param text   　短信内容
     * @param mobile 　接受的手机号
     * @return json格式字符串
     * @throws IOException
     */

    private static String sendSms(String apikey, String text,
        String mobile)  {
        Map < String, String > params = new HashMap < String, String > ();
        params.put("apikey", apikey);
        params.put("text", text);
        params.put("mobile", mobile);
        return HttpUtil.post(URI_SEND_SMS, params);
    }

    // 云片的APIKEY
    private final static String SMS_APIKEY = "473ba028c3ef9172ef866af36ac06b77";
    // 云片的营销类APIKEY
    private final static String SMS_MARKETING_API_KEY = "7b1be5e193d127708fc1e85209140a34";
    /**
     * description: 云片的发送短信
     *
     * @author: leiming
     * @date: 2019-03-12 13:16
     * @param: phone 接收人的手机号
     * @param: templateStr 短信模板文本
     * @param: text 文本内需要替换的内容（可变长参数）
     * @return:
     */
    public static String sendSmsComm(String phone, String templateStr, Object... text){
        return sendSmsComm(SMS_APIKEY, phone, templateStr, text);
    }

    public static String sendMarketingSmsComm(String phone, String templateStr, Object... text){
        return sendSmsComm(SMS_MARKETING_API_KEY, phone, templateStr, text);
    }


    private static String sendSmsComm(String apiKey, String phone, String templateStr, Object... text){
        String msg = String.format(templateStr, text);
        String retStr = sendSms(apiKey, msg, phone);
        if(log.isInfoEnabled()) {
            log.info("发送短信, phone:" + phone + ", msg:" + msg + "\nret:" + retStr);
        }
        return retStr;
    }

    /**
     * 通过模板发送短信(不推荐)
     *
     * @param apikey    apikey
     * @param tpl_id    　模板id
     * @param tpl_value 　模板变量值
     * @param mobile    　接受的手机号
     * @return json格式字符串
     * @throws IOException
     */

    public static String tplSendSms(String apikey, long tpl_id, String tpl_value,
        String mobile) {
        Map < String, String > params = new HashMap < String, String > ();
        params.put("apikey", apikey);
        params.put("tpl_id", String.valueOf(tpl_id));
        params.put("tpl_value", tpl_value);
        params.put("mobile", mobile);
        return HttpUtil.post(URI_TPL_SEND_SMS, params);
    }

    /**
     * 通过接口发送语音验证码
     * @param apikey apikey
     * @param mobile 接收的手机号
     * @param code   验证码
     * @return
     */

    public static String sendVoice(String apikey, String mobile, String code) {
        Map < String, String > params = new HashMap < String, String > ();
        params.put("apikey", apikey);
        params.put("mobile", mobile);
        params.put("code", code);
        return HttpUtil.post(URI_SEND_VOICE, params);
    }

    /**
     * 通过接口绑定主被叫号码
     * @param apikey apikey
     * @param from 主叫
     * @param to   被叫
     * @param duration 有效时长，单位：秒
     * @return
     */

    public static String bindCall(String apikey, String from, String to,
        Integer duration) {
        Map < String, String > params = new HashMap < String, String > ();
        params.put("apikey", apikey);
        params.put("from", from);
        params.put("to", to);
        params.put("duration", String.valueOf(duration));
        return HttpUtil.post(URI_SEND_BIND, params);
    }

    /**
     * 通过接口解绑绑定主被叫号码
     * @param apikey apikey
     * @param from 主叫
     * @param to   被叫
     * @return
     */
    public static String unbindCall(String apikey, String from, String to) {
        Map < String, String > params = new HashMap < String, String > ();
        params.put("apikey", apikey);
        params.put("from", from);
        params.put("to", to);
        return HttpUtil.post(URI_SEND_UNBIND, params);
    }


}